package com.itheima.controller;

import com.itheima.anno.LogAnno;
import com.itheima.dto.LoginDTO;
import com.itheima.pojo.Emp;
import com.itheima.service.EmpService;
import com.itheima.vo.PageBean;
import com.itheima.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
public class EmpController {

    @Autowired
    private EmpService empService;


    //员工分页查询
    @GetMapping("/emps")
    @LogAnno(methodDesc = "员工分页查询")
    public Result findByPage(
            String name,Integer gender,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize
    ){
        PageBean<Emp> pageBean = empService.findByPage(page, pageSize,name,gender,begin,end);
        return Result.success(pageBean);
    }

   //新增员工
    @LogAnno(methodDesc = "新增员工")
    @PostMapping("/epms")
    public Result save(@RequestBody Emp emp){
        empService.save(emp);
        return Result.success();
    }

   //回显员工
    @LogAnno(methodDesc = "回显员工")
    @GetMapping("/emps/{id}")
    public Result findById(@PathVariable("id") Integer id){
        Emp emp = empService.findById(id);
        return Result.success(emp);
    }

    //修改员工
    @PutMapping("/emps")
    @LogAnno(methodDesc = "修改员工")
    public Result update(@RequestBody Emp emp){
        empService.update(emp);
        return Result.success();
    }



    //批量删除
    @LogAnno(methodDesc = "批量删除")
    @DeleteMapping("/emps/{ids}")
    public Result deleteByIds(@PathVariable List<Integer> ids){
        empService.deleteByIds(ids);
        return Result.success();
    }

    //登录
    @LogAnno(methodDesc = "登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginDTO loginDTO){
        //1. 调用service进行登录
        Emp emp=empService.login(loginDTO);
        //2.返回结果
        return Result.success();
    }












}